<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_universitas extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'kd_pt';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*');
			$this->db->from('ref_universitas');
			$this->db->where('status', "A");
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdata_detail($kd_pt){
		$out = '<table class="dv-table" border="0" style="width:100%;">
					<tr>
					<td rowspan="3" style="width:60px">
					<img src="" style="width:60px;margin-right:20px" />
					</td>
					<td class="dv-label">Item ID: </td>
					<td>xx</td>
					<td class="dv-label">Product ID:</td>
					<td>xx</td>
					</tr>
					<tr>
					<td class="dv-label">List Price: </td>
					<td>xx</td>
					<td class="dv-label">Unit Cost:</td>
					<td>xx</td>
					</tr>
					<tr>
					<td class="dv-label">Attribute: </td>
					<td colspan="3">xx</td>
					</tr>
					</table>';
		return $out;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('ref_universitas');
		$this->db->where('kd_pt', $kode);
		
		return $this->db->get();
	}
	
	function getsingledata(){
		$this->db->flush_cache();
		$this->db->select('DISTINCT *',false);
		$this->db->from('ref_universitas');
		
		return $this->db->get()->row();
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('kd_pt',$kode);
		
		$this->db->select('*');
		$this->db->from('ref_universitas');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_pt', $data['kd_pt']);
        $this->db->set('Nama', $data['Nama']);
        $this->db->set('TglMulai', FormatDateToMysql($data['TglMulai']));
        $this->db->set('Alamat1', $data['Alamat1']);
        //$this->db->set('Alamat2', $data['Alamat2']);
        $this->db->set('Kota', $data['Kota']);
        $this->db->set('KodePos', $data['KodePos']);
        $this->db->set('Telepon', $data['Telepon']);
        $this->db->set('Fax', $data['Fax']);
        $this->db->set('Email', $data['Email']);
        $this->db->set('Website', $data['Website']);
        $this->db->set('NoAkta', $data['NoAkta']);
        $this->db->set('TglAkta', FormatDateToMysql($data['TglAkta']));
        $this->db->set('NoSah', $data['NoSah']);
        $this->db->set('TglSah', FormatDateToMysql($data['TglSah']));
		if($data['Logo'] != ''){
			$this->db->set('Logo', $data['Logo']);
        }
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
        $this->db->set('status', isset($data['status'])?'A':'T');
		
		$result = $this->db->insert('ref_universitas');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_pt', $data['kd_pt']);
        $this->db->set('Nama', $data['Nama']);
        $this->db->set('TglMulai', FormatDateToMysql($data['TglMulai']));
        $this->db->set('Alamat1', $data['Alamat1']);
        //$this->db->set('Alamat2', $data['Alamat2']);
        $this->db->set('Kota', $data['Kota']);
        $this->db->set('KodePos', $data['KodePos']);
        $this->db->set('Telepon', $data['Telepon']);
        $this->db->set('Fax', $data['Fax']);
        $this->db->set('Email', $data['Email']);
        $this->db->set('Website', $data['Website']);
        $this->db->set('NoAkta', $data['NoAkta']);
        $this->db->set('TglAkta', FormatDateToMysql($data['TglAkta']));
        $this->db->set('NoSah', $data['NoSah']);
        $this->db->set('TglSah', FormatDateToMysql($data['TglSah']));
		if($data['Logo'] != ''){
			$this->db->set('Logo', $data['Logo']);
        }
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
        $this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->where('kd_pt', $data['kode']);
		$result = $this->db->update('ref_universitas');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		 //query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "T");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('kd_pt', $kode);
		$result = $this->db->update('ref_universitas');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
}

?>